package cn.iocoder.boot.kmeans;

import java.util.ArrayList;
import java.util.List;

public class RecommendationEngine {
    public static List<Talent> recommendTalentsForPosition(List<Cluster> clusters, Talent position) {
        List<Talent> recommendedTalents = new ArrayList<>();
        Cluster bestMatchCluster = null;
        double minDistance = Double.MAX_VALUE;
        for (Cluster cluster : clusters) {
            double distance = position.distanceTo(cluster.getCentroid());
            if (distance < minDistance) {
                minDistance = distance;
                bestMatchCluster = cluster;
            }
        }
        if (bestMatchCluster != null) {
            recommendedTalents.addAll(bestMatchCluster.getTalents());
        }
        return recommendedTalents;
    }

    private static double maxDistance(Talent position, List<Talent> recommendedTalents) {
        double maxDistance = 0.0;
        for (Talent talent : recommendedTalents) {
            double distance = talent.distanceTo(position);
            if (distance > maxDistance) {
                maxDistance = distance;
            }
        }
        return maxDistance;
    }

    public static void recommendTalentsWithReason(List<Cluster> clusters, Talent position, int maxSalary) {
        List<Talent> recommendedTalents = recommendTalentsForPosition(clusters, position);
        List<Talent> affordableTalents = new ArrayList<>();
        for (Talent talent : recommendedTalents) {
            if (talent.getSalary() <= maxSalary && talent.getFeatures()[0] >= 5) {
                affordableTalents.add(talent);
            }
        }

        List<TalentValue> talentValues = new ArrayList<>();
        for (Talent talent : affordableTalents) {
            double value = talent.getFeatures()[0] * 0.5 + talent.getFeatures()[1] * 0.3 + talent.getFeatures()[2] * 0.2;
            talentValues.add(new TalentValue(talent, value));
        }
        talentValues.sort((a, b) -> Double.compare(b.getValue(), a.getValue()));

        System.out.println("\n为企业推荐的人才（在薪资上限内价值最高）：");
        for (int i = 0; i < Math.min(5, talentValues.size()); i++) {
            TalentValue talentValue = talentValues.get(i);
            Talent talent = talentValue.getTalent();
            double matchPercentage = (1 - talent.distanceTo(position) / maxDistance(position, affordableTalents)) * 100;

            System.out.println("  排名 " + (i + 1) + "：");
            System.out.println("    候选人: " + talent.getName());
            System.out.println("    能力: " + talent.toString());
            System.out.println("    与岗位需求的匹配度: " + matchPercentage + "%");
            System.out.println("    价值评分: " + talentValue.getValue());

            StringBuilder reason = new StringBuilder("推荐理由：");
            int[] talentFeatures = talent.getFeatures();
            int[] positionFeatures = position.getFeatures();
            boolean skillMatched = talentFeatures[0] >= positionFeatures[0];
            boolean experienceMatched = talentFeatures[1] >= positionFeatures[1];
            boolean educationMatched = talentFeatures[2] >= positionFeatures[2];

            reason.append("技能匹配度：");
            if (skillMatched) {
                reason.append("该候选人的技能水平达到或超过了岗位要求，能够熟练应对岗位所需的技术挑战。");
            } else {
                reason.append("该候选人的技能水平与岗位要求有差距，但仍具备一定的相关能力，可能需要进一步培训或学习。");
            }

            reason.append(" 经验匹配度：");
            if (experienceMatched) {
                reason.append("经验丰富，能够快速适应岗位并做出贡献。");
            } else {
                reason.append("经验稍有不足，但具有较强的学习能力和成长潜力。");
            }

            reason.append(" 教育背景匹配度：");
            if (educationMatched) {
                reason.append("教育背景符合岗位要求，表明候选人具备扎实的理论基础。");
            } else {
                reason.append("教育背景与岗位要求略有差距，但通过实际工作经验弥补了这一不足。");
            }

            reason.append(" 薪资匹配度：");
            if (talent.getSalary() <= maxSalary * 0.8) {
                reason.append("候选人期望薪资低于企业可支出上限，具有较高的性价比。");
            } else {
                reason.append("候选人期望薪资接近企业可支出上限，需综合评估其技能和经验是否值得该薪资水平。");
            }

            System.out.println("    " + reason.toString());
        }
    }
}